package com.zHan.system.controller.system;


import com.zHan.common.result.Result;
import com.zHan.model.dto.AssginMenuDto;
import com.zHan.model.system.SysMenu;
import com.zHan.system.anno.Log;
import com.zHan.system.menu.BusinessType;
import com.zHan.system.service.ISysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author zHan
 * @since 2023-06-23
 */
@RestController
@Slf4j
@Api(tags = "菜单管理接口")
@RequestMapping("/admin/system/sysMenu")
public class SysMenuController {

    @Autowired
    private ISysMenuService sysMenuService;

    /**
     * 新增菜单
     *
     * @param sysMenu
     * @return
     */
    @PostMapping("/save")
    @PreAuthorize("hasAuthority('bnt.sysMenu.add')")
    @ApiOperation(value = "新增菜单")
    @Log(title = "菜单管理", businessType = BusinessType.INSERT)
    public Result save(@RequestBody SysMenu sysMenu) {
        boolean isSuccess = sysMenuService.save(sysMenu);
        if (isSuccess) {
            return Result.ok();
        }
        return Result.fail();
    }

    /**
     * 删除菜单
     *
     * @param id
     * @return
     */
    @DeleteMapping("/remove/{id}")
    @PreAuthorize("hasAuthority('bnt.sysMenu.remove')")
    @ApiOperation(value = "删除菜单")
    @Log(title = "菜单管理", businessType = BusinessType.DELETE)
    public Result remove(@PathVariable Long id) {
        boolean isSuccess = sysMenuService.removeMenuById(id);
        if (isSuccess) {
            return Result.ok();
        }
        return Result.fail();
    }

    /**
     * 批量删除菜单
     *
     * @param ids
     * @return
     */
    @DeleteMapping("/batchRemove")
    @PreAuthorize("hasAuthority('bnt.sysMenu.remove')")
    @ApiOperation(value = "批量删除菜单")
    @Log(title = "菜单管理", businessType = BusinessType.DELETE)
    public Result batchRemove(@RequestBody List<Long> ids) {
        boolean isSuccess = sysMenuService.batchRemoveMenu(ids);
        if (isSuccess) {
            return Result.ok();
        }
        return Result.fail();
    }

    /**
     * 修改菜单
     *
     * @param sysMenu
     * @return
     */
    @PutMapping("/update")
    @PreAuthorize("hasAuthority('bnt.sysMenu.update')")
    @ApiOperation(value = "修改菜单")
    @Log(title = "菜单管理", businessType = BusinessType.UPDATE)
    public Result update(@RequestBody SysMenu sysMenu) {
        boolean isSuccess = sysMenuService.updateById(sysMenu);
        if (isSuccess) {
            return Result.ok();
        }
        return Result.fail();
    }

    /**
     * 菜单列表（树形）
     *
     * @return
     */
    @GetMapping("/findNodes")
    @ApiOperation(value = "菜单列表")
    public Result findNodes() {
        List<SysMenu> sysMenuList = sysMenuService.selectNodes();
        if (!sysMenuList.isEmpty()) {
            return Result.ok(sysMenuList);
        }
        return Result.fail();
    }

    /**
     * 根据id查询单个菜单
     *
     * @param id
     * @return
     */
    @GetMapping("/findNode/{id}")
    @ApiOperation(value = "查询单个菜单")
    public Result findNode(@PathVariable Long id) {
        SysMenu menu = sysMenuService.getById(id);
        if (menu != null) {
            return Result.ok(menu);
        }
        return Result.fail();
    }

    /**
     * 根据角色获取菜单
     *
     * @param roleId
     * @return
     */
    @GetMapping("/toAssign/{roleId}")
    @ApiOperation(value = "根据角色获取菜单")
    public Result toAssign(@PathVariable String roleId) {
        List<SysMenu> sysMenuList = sysMenuService.findSysMenuByRoleId(roleId);
        if (!sysMenuList.isEmpty()) {
            return Result.ok(sysMenuList);
        }
        return Result.fail();
    }

    /**
     * 给角色分配菜单
     *
     * @param assginMenuDto
     * @return
     */
    @PostMapping("/doAssign")
    @PreAuthorize("hasAuthority('bnt.sysRole.assignAuth')")
    @ApiOperation(value = "给角色分配菜单")
    @Log(title = "角色管理", businessType = BusinessType.OTHER)
    public Result doAssign(@RequestBody AssginMenuDto assginMenuDto) {
        boolean isSuccess = sysMenuService.assignMenuToRole(assginMenuDto);
        if (isSuccess) {
            return Result.ok();
        }
        return Result.fail();
    }

    /**
     * 更改菜单状态
     *
     * @param id
     * @param status
     * @return
     */
    @PutMapping("/updateStatus/{id}/{status}")
    @PreAuthorize("hasAuthority('bnt.sysMenu.update')")
    @ApiOperation(value = "更改菜单状态")
    @Log(title = "菜单管理", businessType = BusinessType.STATUS)
    public Result updateStatus(@PathVariable Long id, @PathVariable Integer status) {
        boolean isSuccess = sysMenuService.updateStatus(id, status);
        if (isSuccess) {
            return Result.ok();
        }
        return Result.fail();
    }
}

